<template>
  <view class="box">
    <!-- 导航 -->
    <uv-navbar title="收货地址" bgColor="#f7f6fa" height="44px" titleStyle="{font-weight: bold;font-size: 1.1rem;}"
      @click="goback"></uv-navbar>
    <view class="contact">
      <view class="one">
        <view class="left">收货人：</view>
        <input type="text" :placeholder="list.name" v-model="name1" />
      </view>
      <view class="one">
        <view class="left">手机号：</view>
        <input type="text" :placeholder="list.mobile" v-model="mobile" />
      </view>
      <view class="one">
        <view class="left">选择地区：</view>
        <input type="text" v-model="name" :placeholder="list.area" />
        <uv-picker ref="picker" :columns="columns" @confirm="confirm"></uv-picker>
        <uv-icon name="arrow-right" size="18px" @click="openPicker"></uv-icon>

      </view>
      <view class="one">
        <view class="left">详细地址:</view>
        <input type="text" :placeholder="list.address" v-model="address" />
      </view>
      <view class="one">
        <view class="left">设为默认地址:</view>
        <view class="right">
          <uv-radio-group>
            <uv-radio customStyle="{text-align: right;}" name="2" activeColor="#008c8c" @change="change"></uv-radio>
          </uv-radio-group>
        </view>
      </view>
    </view>
    <!-- 按钮 -->
    <view class="btn">
      <uv-button type="primary" color="#008c8c" shape="circle" text="新增地址" @click="jum"></uv-button>
    </view>
  </view>
</template>

<script>
  import {
    uniRequest
  } from "../../utils/request.js"
  export default {
    data() {
      return {
        name: '',
        name1: '',
        mobile: '',
        address: '',
        switch1: '',
        list: '',
        columns: [
          ['济南', '青岛', '淄博', '枣庄', '东营', '烟台', '潍坊', '济宁', '泰安', '威海', '日照', '滨州', '德州', '聊城', '临沂', '菏泽']
        ],

      }
    },
    created() {
      const id = this.$route.query.id
      const that = this
      uniRequest('address/edit?server=1', function(err, data) {
        if (err) {
          // 错误
          console.log(err)
        } else {
          console.log('"成功了"', data)
          that.list = data
        }
      }, 'POST', {
        id: id
      });
    },
    methods: {
      // 用户地址添加
      jum() {
        const id = this.$route.query.id
        const name = this.name1
        const mobile = this.mobile
        const area = this.name[0]
        const address = this.address
        const sw = this.switch1
        console.log(id, name, mobile, area, address, sw)
        if (id == 'undefined') {
          uniRequest('address/add?server=1', function(err, data) {
            if (err) {
              // 错误
              console.log(err)
            } else {
              console.log('"成功了"', data)
            }
          }, 'POST', {
            name: name,
            mobile: mobile,
            area: area,
            address: address,
            switch: sw
          });
        } else {
          console.log('6666')
          if (sw == 2) {
            console.log('2222222222222222')
            uniRequest('address/switch?server=1', function(err, data) {
              if (err) {
                // 错误
                console.log(err)
              } else {
                console.log('"成功了"', data)
              }
            }, 'POST', {
              name: name,
              mobile: mobile,
              area: area,
              address: address,
              id: id
            });
          } else {
            uniRequest('address/update?server=1', function(err, data) {
              if (err) {
                // 错误
                console.log(err)
              } else {
                console.log('"成功了"', data)
              }
            }, 'POST', {
              name: name,
              mobile: mobile,
              area: area,
              address: address,
              id: id
            });
          }
        }
        // uni.navigateTo({
        //   url:'/pages/deliveryaddress/deliveryaddress'
        // })
      },
      change(e) {
        this.switch1 = e
        console.log(this.switch1)
      },
      // 联动
      openPicker() {
        this.$refs.picker.open();
      },
      confirm(e) {
        this.name = e.value
        console.log('confirm', e);
      },
      goback() {
        uni.navigateBack({
          delta: 1
        });
      },

    }
  }
</script>

<style lang="scss">
  @import url("newaddress.scss");
</style>